package juf.adapter;

import java.awt.Dimension;
import java.util.Date;

import javax.swing.*;
import juf.*;
/**Binding between a JFormattedTextField component and a Property data source
 * 
 * @author Dale Halliwell
 *
 */
@SuppressWarnings("serial")
public class JTextFieldAdapter extends AbstractAdapter implements Adapter {

	
	public JTextFieldAdapter(Property property) { 
		this.component = new JFormattedTextField();
		((JFormattedTextField)this.component).setColumns(20); //default width
		bind(property, new Dimension(350,30)); //default size
	}
	

	/*
	 * (non-Javadoc)
	 * @see juf.Adapter#refresh()
	 */
	@Override
	public void refresh(){
		this.clearInvalidWarning(); //clears any warnings for invalid data 
		
		if (!(property.getValue() == null))
			((JFormattedTextField)component).setText(property.getValue().toString());
	}
	
	
	/* 
	 * (non-Javadoc)
	 * @see juf.Adapter#update()
	 */
	@Override
	public void save(){
		//this textbox can be used to display many different types, these should all be tested first
		try{
			if (property.getType() == int.class || property.getType() == Integer.class)
				property.setValue(Integer.parseInt(((JFormattedTextField)component).getText()));
			else if (property.getType() == float.class)
				property.setValue(Float.parseFloat(((JFormattedTextField)component).getText()));
			else if (property.getType() == Double.class || property.getType() == double.class)
				property.setValue(Double.parseDouble(((JFormattedTextField)component).getText()));					
			else property.setValue(((JFormattedTextField)component).getText());
			
			this.clearInvalidWarning(); //saved successfully, so clear any invalid warning
		}
		catch (IllegalArgumentException e ){ //if the argument is of the wrong type in any case		
			this.showInvalidWarning();//show the invalid warning, should also raise an exception to the Jufform to display a warning?
		}
	}

	/* (non-Javadoc)
	 * @see adapters.Adapter#validateField()
	 */
	@Override	
	public boolean validateField(){
		
		//this textbox can be used to display many different types, these should all be tested first
		try{
			if (property.getType() == int.class || property.getType() == Integer.class)
				Integer.parseInt(((JFormattedTextField)component).getText());
			else if (property.getType() == float.class)
				Float.parseFloat(((JFormattedTextField)component).getText());
			else if (property.getType() == Double.class || property.getType() == double.class)
				Double.parseDouble(((JFormattedTextField)component).getText());			
			else if (property.getType() == String.class){
				//TODO:should we do something - e.g. ensure name field has only text?
			}
			this.clearInvalidWarning(); //clear any invalid warning from before
		}
		catch (IllegalArgumentException e ){ //if there is a argument not of expected type		
			this.showInvalidWarning();//show the invalid warning
			return false;
		}
		
		return true;
	}








}
